package com.xiaomei.manager.web.jpush;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.audience.AudienceTarget;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

import com.xiaomei.manager.utils.Constants;

import java.util.List;

public class PushExample {

	private static String appKey = "0efd29eaa86e3eb990f8f375";
	private static String masterSecret = "42d9e04422ccb9b7fa93fbcd";



	public static void doPush(PushPayload payload) throws Exception {
		JPushClient jpushClient = new JPushClient(masterSecret, appKey);
		try {
			PushResult result = jpushClient.sendPush(payload);
			System.out.println("Got result - " + result);
		} catch (APIConnectionException e) {
			// Connection error, should retry later
			System.out.println("Connection error, should retry later");
			throw new APIConnectionException("Connection error, should retry later", e);
		} catch (APIRequestException e) {
			// Should review the error, and fix the request
			System.out.println("Should review the error, and fix the request");
			System.out.println("HTTP Status: " + e.getStatus());
			System.out.println("Error Code: " + e.getErrorCode());
			System.out.println("Error Message: " + e.getErrorMessage());
			throw new Exception("异常");
		}
	}

	/**
	 * eg:
	 * @param tagName @"man" : @"woman", @"driver"
	 * @param Content
	 * @param MessageType
	 * @param MessageState
	 * @throws Exception
	 */
	public static void JpushByTag(String tagName,String Content, String MessageType, String MessageState) throws Exception{
		// For push, all you need do is to build PushPayload object.
		PushPayload payload = buildPushObjectByTag(tagName,Content,MessageType,MessageState);
		doPush(payload);
	}

	public static void JpushAllUser(String Content, String MessageType, String MessageState) throws Exception{
		// For push, all you need do is to build PushPayload object.
		PushPayload payload = buildPushObjectAllUser(Content,MessageType,MessageState);
		doPush(payload);
	}

 	public static void JpushByAlias(String user,String Content, String MessageType, String MessageState) throws Exception{
		// For push, all you need do is to build PushPayload object.
        PushPayload payload = buildPushObject_ios_tagAnd_alertWithExtrasAndMessage(user,Content,MessageType,MessageState);
		doPush(payload);
	}

	public static void JpushByUsercodes(List<String> users,String Content, String MessageType, String MessageState) throws Exception{
		// For push, all you need do is to build PushPayload object.
		PushPayload payload = buildPushObject_byUserCodes(users,Content,MessageType,MessageState);
		doPush(payload);
	}

	/**
	 * 根据别名来批量推送
	 * @param tag
	 * @param Content
	 * @param MessageType
	 * @param MessageState
     * @throws Exception
     */

	public static PushPayload buildPushObjectByTag(String tag, String Content, String MessageType,
																				   String MessageState) {
		return PushPayload.newBuilder()
				.setPlatform(Platform.all())
				.setAudience(Audience.newBuilder()
						.addAudienceTarget(AudienceTarget.tag(tag)).build())
				.setNotification(Notification.newBuilder()
						.addPlatformNotification(IosNotification.newBuilder()
								.setAlert(Content)
								//     .setBadge(+1)
								.setSound("happy.caf")
								.addExtra("MessageType", MessageType)
								.addExtra("MessageState", MessageState)
								.build())
						.build())
//                  .setNotification(Notification.newBuilder()
//                        .addPlatformNotification(AndroidNotification.newBuilder()
//                                .setAlert(alertContent)
//                                .setTitle("haha")
//                                .addExtra("MessageType", MessageType)
//                                .addExtra("MessageState", MessageState)
//                                .build())
//                        .build())
				.setMessage(Message.newBuilder()
						.setMsgContent(Content)
						.addExtra("MessageType", MessageType)
						.addExtra("MessageState", MessageState)
						.build())
				.setOptions(Options.newBuilder()
						.setApnsProduction(Constants.PUSH_ENVIRONMENT)  //生产环境或者开发环境
						.build())
				.build();
	}

	public static PushPayload buildPushObjectAllUser(String Content, String MessageType,
												   String MessageState) {
		return PushPayload.newBuilder()
				.setPlatform(Platform.all())
				.setAudience(Audience.newBuilder()
						.setAll(true).build())
				.setNotification(Notification.newBuilder()
						.addPlatformNotification(IosNotification.newBuilder()
								.setAlert(Content)
								//     .setBadge(+1)
								.setSound("happy.caf")
								.addExtra("MessageType", MessageType)
								.addExtra("MessageState", MessageState)
								.build())
						.build())
//                  .setNotification(Notification.newBuilder()
//                        .addPlatformNotification(AndroidNotification.newBuilder()
//                                .setAlert(alertContent)
//                                .setTitle("haha")
//                                .addExtra("MessageType", MessageType)
//                                .addExtra("MessageState", MessageState)
//                                .build())
//                        .build())
				.setMessage(Message.newBuilder()
						.setMsgContent(Content)
						.addExtra("MessageType", MessageType)
						.addExtra("MessageState", MessageState)
						.build())
				.setOptions(Options.newBuilder()
						.setApnsProduction(Constants.PUSH_ENVIRONMENT)  //生产环境或者开发环境
						.build())
				.build();
	}

	/**
	 * 给IOS平台客户端用户别名推送通知消息
	 * @return
	 */
	public static PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage(String user, String Content, String MessageType, 
			String MessageState) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.newBuilder()
                        .addAudienceTarget(AudienceTarget.alias(user)).build())
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(Content)
                         //     .setBadge(+1)
                                .setSound("happy.caf")
                                .addExtra("MessageType", MessageType)
                                .addExtra("MessageState", MessageState)
                                .build())
                        .build())
//                  .setNotification(Notification.newBuilder()
//                        .addPlatformNotification(AndroidNotification.newBuilder()
//                                .setAlert(alertContent)
//                                .setTitle("haha")                                
//                                .addExtra("MessageType", MessageType)
//                                .addExtra("MessageState", MessageState)
//                                .build())
//                        .build())
                 .setMessage(Message.newBuilder()
	                        .setMsgContent(Content)
	                        .addExtra("MessageType", MessageType)
                            .addExtra("MessageState", MessageState)
	                        .build())
                 .setOptions(Options.newBuilder()
                         .setApnsProduction(Constants.PUSH_ENVIRONMENT)  //生产环境或者开发环境
                         .build())
                 .build();
    }


	/**
	 * 给IOS平台客户端用户别名推送通知消息
	 * @return
	 */
	public static PushPayload buildPushObject_byUserCodes(List<String> users, String Content, String MessageType,
														  String MessageState) {
		return PushPayload.newBuilder()
				.setPlatform(Platform.all())
				.setAudience(Audience.newBuilder()
						.addAudienceTarget(AudienceTarget.alias(users)).build())
				.setNotification(Notification.newBuilder()
						.addPlatformNotification(IosNotification.newBuilder()
								.setAlert(Content)
								//     .setBadge(+1)
								.setSound("happy.caf")
								.addExtra("MessageType", MessageType)
								.addExtra("MessageState", MessageState)
								.build())
						.build())
//                  .setNotification(Notification.newBuilder()
//                        .addPlatformNotification(AndroidNotification.newBuilder()
//                                .setAlert(alertContent)
//                                .setTitle("haha")
//                                .addExtra("MessageType", MessageType)
//                                .addExtra("MessageState", MessageState)
//                                .build())
//                        .build())
				.setMessage(Message.newBuilder()
						.setMsgContent(Content)
						.addExtra("MessageType", MessageType)
						.addExtra("MessageState", MessageState)
						.build())
				.setOptions(Options.newBuilder()
						.setApnsProduction(Constants.PUSH_ENVIRONMENT)  //生产环境或者开发环境
						.build())
				.build();
	}

}
